var ImgManage = Class.create({

    successList : {}, //加载成功资源列表  
    errorList : {},   //加载失败资源列表  

    initialize: function() {
        this.resNumber = 0; //需加载资源总数
        this.completeNumber = 0; //完成加载资源数
        this.successNum = 0; //加载成功资源数
        this.errorNum = 0; //加载失败资源数

        document.on('img:load', this.onLoad.bind(this));
        document.on('img:error', this.onError.bind(this));

    },
    /*
	 * @function	加载资源包
	 * @parameter	img_rows	资源信息数组
	 */
    addResourcesBag: function(img_rows) {
        /*
        console.debug(1)
        if (! (img_rows && img_rows.length > 0)) return;
        this.resNumber = img_rows.length;
        this.completeNumber = 0;
        this.successNum = 0;
        this.errorNum = 0;

        for (var i = 0; i < img_rows.length; i++) {

            var lb = new ImgLoad();
            lb.imgRow = img_rows[i];;

            lb.setImageUrl(img_rows[i][1]);
        }
        */
        //console.debug(1)
        if (! (img_rows && img_rows.length > 0)) return;
        this.resNumber = img_rows.length;
        this.completeNumber = 0;
        this.successNum = 0;
        this.errorNum = 0;

        for (var i = 0; i < img_rows.length; i++) {
            if (!Object.isUndefined(this.successList[img_rows[i][0]]))
            {
                this.resNumber --;
            }
        }
        
        if (0 == this.resNumber)
        {
            this.totalResourceLoad();
        }

        for (var i = 0; i < img_rows.length; i++) {
            if (Object.isUndefined(this.successList[img_rows[i][0]]))
            {
                var lb = new ImgLoad();
                lb.imgRow = img_rows[i];;

                lb.setImageUrl(img_rows[i][1]);
            }
            else
            {

            }
        }
    },

    /*
	 * @function	加载后处理
	 * @parameter	img_row	资源ImgLoad对象
	 */
    loadTransact: function(ca) {
        this.completeNumber += 1;
    },

    /*
	 * @function 	资源加载成功处理
	 * @parameter	ev	事件
	 */
    onLoad: function(ev) {
        //console.info(ev.memo.imgRow[0]);

        this.loadTransact(ev.memo.imgRow);
        this.successNum += 1;
        this.successList[ev.memo.imgRow[0]] = ev.memo; //保存资源至加载成功资源列表

		//IE6下缓存
		if(LLBrowserInfo.isIE()&& LLBrowserInfo.getBrowserVersion()<7)
		{
			var vImage= document.createElement('<v:image />');
			with(vImage.style){
				position='absolute';
				left=top='-1000px';
				display='none';
			}
			vImage.src=ev.memo.imgRow[1];
			document.body.appendChild(vImage);	
		}

        this.checkTotalLoad();
    },

    /*
	 * @function 	资源加载失败处理
	 * @parameter	ev	事件
	 */
    onError: function(ev) {
        this.loadTransact(ev.memo.imgRow);
        this.errorNum += 1;
        this.errorList[ev.memo.imgRow[0]] = ev.memo; //保存资源信息至加载失败资源列表

        this.checkTotalLoad();
    },

    /*
	 * @function	检测资源是否已全部处理
	 */
    checkTotalLoad: function() {
        document.fire("img:oneloaded", [this.completeNumber, this.resNumber]);

        //console.info(this.successNum);
        //console.info(this.errorNum);
        //console.info(this.resNumber);
        if ((this.successNum + this.errorNum) == this.resNumber) {
            if (0 == this.errorNum) {
                this.totalResourceLoad([this.completeNumber, this.resNumber]);
            } else {
                console.info('部分资源加载失败');
                console.info(this.errorList);
            }
        }
    },

    /*
	 * @function	资源加载完毕处理
	 */
    totalResourceLoad: function(memo) {
        console.info('全部加在成功');
        document.fire("img:complete", memo);
        //LLEvent.trigger(this,'totalloaded',[]);
    },

    getResource: function(ca) {
        return this.successList[ca];
    }

});



ImgManage._instance = null;
ImgManage.getInstance = function()
{
	if(null == ImgManage._instance)
    {
		ImgManage._instance = new ImgManage();
	}

    return ImgManage._instance;
}